package cn.sms.product.controller;

import cn.sms.basic.exception.BusinessException;
import cn.sms.basic.util.AjaxResult;
import cn.sms.basic.util.LoginContext;
import cn.sms.basic.util.PageList;
import cn.sms.org.domain.Employee;
import cn.sms.org.service.IEmployeeService;
import cn.sms.pet.domain.Pet;
import cn.sms.product.domain.Product;
import cn.sms.product.domain.Product;
import cn.sms.product.query.ProductQuery;
import cn.sms.product.service.IProductService;
import cn.sms.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;
    @Autowired
    private IEmployeeService employeeService;

    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            productService.onsale(ids);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            productService.remove(id);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    @PostMapping("/offsale")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            productService.offSale(ids);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    @GetMapping("/{productId}")
    public Product getProduct(@PathVariable("productId") Long productId){
        return productService.queryByIdDetail(productId);
    }

    /*@GetMapping("/getProduct/{id}")
    public Product getProduct(@PathVariable("id") Long id){
        return productService.;
    }*/

    @PutMapping
    public AjaxResult goSave(@RequestBody Product product, HttpServletRequest request){
        try {
            Logininfo loginInfo = LoginContext.getLoginInfo(request);
            productService.handle(product,loginInfo);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }
    }

    @PostMapping
    public PageList<Product> queryPage(@RequestBody ProductQuery productQuery,HttpServletRequest request){
        /*Logininfo loginInfo = LoginContext.getLoginInfo(request);
        Employee employee = employeeService.ByLogininfoId(loginInfo.getId());
        if(employee.getShop_id() != null){
            productQuery.setShopId(employee.getShop_id());
        }*/
        return productService.queryPage(productQuery);
    }



}
